package com.hb.trade.api.im.chat.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.trade.api.im.chat.manager.TradeLayImUser;
import com.hb.trade.api.im.chat.service.TradeImChatService;
import com.hb.trade.api.im.hall.handler.HallMessageHandler;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.model.websocket.SocketMessageEntity;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 即时通讯用户记录表 前端控制器
 * </p>
 *
 * @author zhaojk
 * @since 2021-12-30
 */
@Slf4j
@Version(1.0)
@RequestMapping("/{version}")
@RestController
public class ChatUserController {


    protected final TradeImChatService tradeImChatService;

    public ChatUserController(TradeImChatService tradeImChatService) {
        this.tradeImChatService = tradeImChatService;
    }

    /**
     * 群发消息
     *
     * @param messageEntity#contentData#msg 消息内容
     * @param messageEntity#id              标段ID
     * @return
     */
    @ApiOperation(value = "群发消息", notes = "群发消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageEntity#contentData#msg", value = "消息内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "messageEntity#id", value = "标段Id", dataTypeClass = Long.class)
    })
    @PostMapping("/layim/sendMultipleMsg")
    public ResponseDto sendMultipleMsg(@RequestBody SocketMessageEntity messageEntity) {
        Long sectionId = messageEntity.getId();
        if (sectionId == null) {
            log.warn("sendMultipleMsg failed for sectionId is null");
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }

        // 设置标段ID, 如果没有该值, 消息将被忽略, 不予发送
        JSONObject dataContent = JSONUtil.createObj();
        dataContent.putOpt("sectionId", sectionId);

        // 设置发送人对象信息
        User agencyUser = tradeImChatService.getAgencyUser(sectionId);
        TradeLayImUser mine = new TradeLayImUser(agencyUser, true);
        mine.setContent(messageEntity.getContentData().getStr("msg"));
        dataContent.putOpt("mine", mine);

        // 查询群成员
        List<User> users = tradeImChatService.listGroupMembers(sectionId);
        if (CollUtil.isEmpty(users)) {
            log.warn("sendMultipleMsg failed for can not get trade user, sectionId is [{}]", sectionId);
            return ResultUtil.fail("未能获取投标人信息");
        }

        log.info("sendMultipleMsg users is [{}]", users);
        for (User user : users) {
            TradeLayImUser to = new TradeLayImUser(user);
            to.setType("friend");
            dataContent.putOpt("to", to);
            HallMessageHandler.handleIm(dataContent);
        }

        return ResultUtil.SUCCESS;
    }
}
